package consumer5;

import java.util.Queue;

public class Consumer implements Runnable {
	Queue<Integer> q;
	String name;

	public Consumer(Queue<Integer> q, String name) {
		this.q = q;
		this.name = name;
	}

	@Override
	public void run() {
		try {
			while (true) {

				synchronized (q) {
					if (q.isEmpty() == false) {
						consume(q.poll());
						q.notifyAll();
					} else
						q.wait();
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void consume(Integer take) {
		System.out.println(this.name + ">>" + take);
	}
}